<template>
  <view class="house">
    <life-empty v-if='!repairList.length' url='/pkg-repair/add-repair/add-repair' text="您还没有保修信息，请点击"></life-empty>
    <view v-else class="house-item">
      <repair-item v-for="item in repairList" :key="item.id" :list='item' @stutas='onStutas' @click='goDetail'>
      </repair-item>
      <life-button icon="../../static/icon/repair.png" @click='goadd'>在线报修</life-button>
    </view>
    <!-- 修改状态 -->
    <u-action-sheet :actions="list" title="修改状态" :show="show1" closeOnClickOverlay @close="close" @select="select">
    </u-action-sheet>
  </view>
</template>

<script>
  import {
    updateStatus
  } from '../../api/house.js'
  import {
    getRepairList
  } from '@/api/repair.js'
  export default {
    data() {
      return {
        repairList: [],
        query: {
          current: 1,
          pageSize: 20
        },
        flag: false,
        // 弹出
        show1: false,
        list: [{
            name: '受理中',
            id: "1"
          },
          {
            name: '上门中',
            id: "2"
          },
          {
            name: '已完成',
            id: "3"
          }, {
            name: '已取消',
            id: "0"
          }
        ],
        query: {}
      }
    },
    onLoad() {
      this.getrepairList()

    },
    methods: {
      async select(e) {
        // console.log(e)
        this.query.status = e.id
        const res = await updateStatus(this.query.type, this.query.id, this.query.status)
        await this.downRefresh()
      },
      // 关闭
      close() {
        this.show1 = false
      },
      // 修改状态
      onStutas(id) {
        console.log(id)
        this.query = {
          status: "",
          id,
          type: "repair"
        }
        this.show1 = true
      },
      async getrepairList() {
        const res = await getRepairList(this.query)
        // const arr = res.data.rows.map(item => {
        //   if (Object.prototype.toString.call(item.attachment) === "[object Object]") {
        //     item.attachment = [item.attachment]
        //   }
        //   return item
        // })
        // console.log(arr)
        this.pageTotal = res.data.pageTotal
        // console.log(this.pageTotal)
        this.repairList = [...this.repairList, ...res.data.rows]
      },
      // 去详情
      goDetail(id) {
        // console.log(id)
        uni.navigateTo({
          url: '/pkg-repair/repair-detail/repair-detail?id=' + id
        })
      },
      goadd() {
        uni.navigateTo({
          url: '/pkg-repair/add-repair/add-repair'
        })
      },
      // 下拉函数
      async downRefresh() {
        // console.log('上拉了')
        this.repairList = []
        this.query = {
          current: 1,
          pageSize: 10
        }
        await this.getrepairList()
        uni.stopPullDownRefresh()
      },

    },
    // 下拉
    onPullDownRefresh() {
      this.downRefresh()
    },
    // 触底
    async onReachBottom() {
      if (this.query.current === this.pageTotal || this.flag) return
      this.flag = true
      this.query.current++
      await this.getrepairList()
      this.flag = false
    },

  }
</script>

<style lang="scss" scoped>
  .house {
    height: 100%;
    background-color: #FAFAFA;
  }

  .house-item {
    padding: 40rpx;
    padding-bottom: 300rpx;
  }
</style>
